/**
 * 控制按钮事件
 */
const events = {
    // 检查重复书签
    getRepetition: function () {
        console.log('检查重复');
        // 1 获取全部书签
        bookmark.getTree()
            .then((tree) => {
                // 获取全部到一个数组
                let arr = page.treeToArray(tree);
                // 书签数组重复项
                let repetition = page.repetition(arr);
                // 把重复项add to page
                page.repetitionToPage(repetition);
            });
    },
    // 检查重复书签文件夹
    delRepetition: function () {
        console.log('检查重复，自动删除');
        bookmark.getTree()
            .then((tree) => {
                // 获取全部到一个数组
                let arr = page.treeToArray(tree);
                // 书签数组重复项
                let repetition = page.repetition(arr);
                // 删除重复项
                for (let i = 1; i < repetition.length; i++) {
                    const element = arr[i];
                    bookmark.removeBookmark(element.id);
                }
            });
    },
    // 增加一个文件夹
    addFolder: function (title = 'new folder', parentId = '1') {
        if (!title) {
            return alert('请输入标题');
        }

        bookmark.add({ title, parentId });
    },
    // 增加一个书签
    addBookmark: function (title, url, parentId = '1') {
        if (!title) {
            return alert('请输入标题');
        } else if (!rul) {
            return alert('请输入URL');
        }

        bookmark.add({ title, parentId, url });
    },
    // 获取全部书签树
    getTree: function () {
        bookmark.getTree()
            .then((tree) => {
                console.log(tree);
            });
    },
    getFolder: function () {
        bookmark.getFolder()
            .then((list) => {
                console.log(list);
            });
    },
    getBookmark: function () {
        bookmark.getBookmark()
            .then((list) => {
                console.log(list);
            });
    },
    // 删除书签
    removeBookmark: function (ev) {
        ev.preventDefault();
        let id = this.getAttribute('data-id');
        bookmark.removeBookmark(id)
            .then(() => { page.removeLi(this) })
            .catch(() => { alert('删除失败了') });
    }
}
